package sxh.springMvc;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class ImageInput {
	
	 @RequestMapping(value = "uploadInit")  
	    public String upload() {

		 return "updaload";
	 } 
    @RequestMapping(value = "upload")  
    public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,ModelMap
    		model) {  
  
        System.out.println("开始");  
        String path = request.getSession().getServletContext().getRealPath("/")+"upload";  
        String fileName = file.getOriginalFilename();  
//        String fileName = new Date().getTime()+".jpg";  
        System.out.println(path);  
        // 文件保存路径  
        File targetFile = new File(path, fileName);  
        if(!targetFile.exists()){  
            targetFile.mkdirs();  
        }  
  
        //保存  
        try {  
            file.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        model.addAttribute("fileUrl", "upload/"+fileName);  
  
        return "result";  
    } 
    @RequestMapping(value = "delFile")  
    public String delFile( String filename,HttpServletRequest request) {
         File file=new File( request.getSession().getServletContext().getRealPath("/")+filename);
     if(file.exists()){
    	 file.delete();
    	 System.out.println("删除成功");
     }
	 return "updaload";
 } 
}
